package com.huadao.pda.project.projectList;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.base.BaseListFragment;
import com.huadao.pda.base.BasePresenter;
import com.huadao.pda.base.OnScanResult;
import com.huadao.pda.common.GsonUtil;
import com.huadao.pda.common.adapter.CommonRecycleAdapter;
import com.huadao.pda.common.adapter.CommonViewHolder;
import com.huadao.pda.common.adapter.FooterHolder;
import com.huadao.pda.common.adapter.HeaderHolder;
import com.huadao.pda.common.adapter.onItemCommonClickListener;
import com.huadao.pda.data.bean.DeviceInfo;
import com.huadao.pda.data.bean.Project;
import com.huadao.pda.main.scanView.ScanDF;
import com.huadao.pda.project.bindingProject.BindingProjectDF;
import com.huadao.pda.project.projectDetail.ProjectDetailDF;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ProjectListDF extends BaseListFragment implements IProjectListContract.IProjectListView, BindingProjectDF.OnBind {

    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.btn_logout)
    Button btn_logout;
    @BindView(R.id.closeSearch)
    ImageView closeSearch;
//    @BindView(R.id.sw_search)
//    SearchView sw_search;

    private List<Project> projectList;
    private ProjectPresenter presenter;

    @Override
    protected void init() {
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s!=null&&s.length()>0){
                    closeSearch.setVisibility(View.VISIBLE);
                }else {
                    closeSearch.setVisibility(View.GONE);
                }
            }
        });
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    et_search.requestFocus();
                    String condition=v.getText().toString();
                    presenter.searchProjectList(condition);
                    return true;
                } else {
                    return false;
                }
            }
        });
        projectList=new ArrayList<>();
        presenter=new ProjectPresenter(this);
        loadData();
        closeSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search.setText("");
                loadData();
            }
        });

//        sw_search.setIconified(false);
    }

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return null;
    }

    @Override
    protected CommonRecycleAdapter setAdapter() {
        return new ProjectAdapter(getActivity(),projectList,R.layout.item_project);
    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(getActivity());
    }

    @Override
    protected void loadData() {
        presenter.getProjectList();
    }

    @Override
    protected void loadMoreData() {
        presenter.getProjectList();
    }

    @Override
    public void onGetProjectList(List<Project> projectList) {
        setSwipeRefresh(false);
        this.projectList.clear();
        this.projectList.addAll(projectList);
        notifyDataSetChanged();
    }

    @Override
    public void onbindSucc(Project project, DeviceInfo deviceInfo) {
        //刷新列表
        presenter.getProjectList();
        project.setDeviceId1(deviceInfo.getId());
        ProjectDetailDF projectDetailDF=ProjectDetailDF.newInstance(project);
        projectDetailDF.show(getFragmentManager(),null);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_project_list;
    }

    @Override
    public int getRecycleViewId() {
        return R.id.recycleView;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return R.id.SwipeRefreshLayout;
    }

    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }

    class ProjectAdapter extends CommonRecycleAdapter<Project>{

        private onItemCommonClickListener onItemCommonClickListener=new onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                final Project project=projectList.get(position);
                if(project.isBinded()){
                    ProjectDetailDF projectDetailDF=ProjectDetailDF.newInstance(project);
                    projectDetailDF.show(getFragmentManager(),null);
                }else {
                    ScanDF scanDF=new ScanDF();
                    scanDF.setOnScanResult(new OnScanResult() {
                        @Override
                        public void onScanResult(String barcode) {
                            try {
                                DeviceInfo deviceInfo=GsonUtil.getInstance().getGson().fromJson(barcode,DeviceInfo.class);
                                BindingProjectDF bindingProjectDF=BindingProjectDF.newInstance(deviceInfo,project);
                                bindingProjectDF.setOnBind(ProjectListDF.this);
                                bindingProjectDF.show(getFragmentManager(),null);
                            }catch (Exception e){
                                e.printStackTrace();
                                showToast("设备信息有误");
                            }
                        }
                    });
                    scanDF.show(getFragmentManager(),null);
                }
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        };

        public ProjectAdapter(Context context, List<Project> dataList, int layoutId) {
            super(context, dataList, layoutId);
        }

        @Override
        protected void bindData(CommonViewHolder holder, Project data,int position) {
            holder.setText(R.id.tv_NO,String.valueOf(position+1));
            holder.setText(R.id.tv_uic,data.getIdentifier());
            if(data.isBinded()){
                holder.setText(R.id.tv_status,getString(R.string.alreadyBinding));
                holder.setTextColor(R.id.tv_status,getResources().getColor(R.color.text_blue));
            }else {
                holder.setText(R.id.tv_status,getString(R.string.notBinding));
                holder.setTextColor(R.id.tv_status,getResources().getColor(R.color.orange));
            }
            holder.setCommonClickListener(onItemCommonClickListener);
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {

        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }

    }

    @Override
    public void onDestroyView() {
        presenter.detachView();
        super.onDestroyView();
    }

}
